//
//  FFFontSizeViewController.swift
//  Clock
//
//  Created by jisa on 2020/5/8.
//  Copyright © 2020 jff. All rights reserved.
//

import UIKit

typealias FFBlock = () -> Void

class FFFontSizeViewController: UIViewController {

    fileprivate var topView:UIView!
    fileprivate var titleLabel:UILabel!
    fileprivate var completeBtn:UIButton!
    
    fileprivate var sizeOf80View:FFAcquireSimpleView!
    fileprivate var sizeOf90View:FFAcquireSimpleView!
    fileprivate var sizeOf100View:FFAcquireSimpleView!
    fileprivate let sizeArr:[Int] = [80, 90, 100]
    fileprivate var selectIndex:Int = 1
    private var list:[FFAcquireSimpleView] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        FFSetUpSubviews()
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        topView.frame = CGRect(x: 0, y: 0, width: view.bounds.size.width, height: 44 * ScaleOf375)
        titleLabel.frame = topView.bounds
        completeBtn.frame = CGRect(x: view.bounds.size.width - 60 * ScaleOf375, y: 7 * ScaleOf375, width: 60 * ScaleOf375, height: 30 * ScaleOf375)
        if UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight {
            let needWidth = 180 * ScaleOf375
            let left = (view.bounds.size.width - needWidth * 3) / 4.0
            let top = (view.bounds.size.height - 120 * ScaleOf375) / 2.0
            sizeOf80View.frame = CGRect(x: left, y: top, width: 180 * ScaleOf375, height: 120 * ScaleOf375)
            sizeOf90View.frame = CGRect(x: left * 2 + needWidth, y: top, width: 180 * ScaleOf375, height: 120 * ScaleOf375)
            sizeOf100View.frame = CGRect(x: left * 3 + needWidth * 2, y: top, width: 180 * ScaleOf375, height: 120 * ScaleOf375)
        }else {
            let left = (ScreenWidth - 180 * ScaleOf375) / 2.0
            let top = (view.bounds.size.height - 44 * ScaleOf375 - 120 * ScaleOf375 * 3) / 4.0
            sizeOf80View.frame = CGRect(x: left, y: 44 * ScaleOf375 + top, width: 180 * ScaleOf375, height: 120 * ScaleOf375)
            sizeOf90View.frame = CGRect(x: left, y: 164 * ScaleOf375 + top * 2, width: 180 * ScaleOf375, height: 120 * ScaleOf375)
            sizeOf100View.frame = CGRect(x: left, y: 284 * ScaleOf375 + top * 3, width: 180 * ScaleOf375, height: 120 * ScaleOf375)
        }
    }
    
    fileprivate func FFSetUpSubviews() {
        view.backgroundColor = FFColorRGB(242, 242, 242)
                
        sizeOf80View = FFAcquireSimpleView(80) { [weak self] in
            self?.FFSelectChange(0)
        }
        sizeOf80View.needHiddenImage = true
        sizeOf90View = FFAcquireSimpleView(90) { [weak self] in
            self?.FFSelectChange(1)
        }
        sizeOf100View = FFAcquireSimpleView(100) { [weak self] in
            self?.FFSelectChange(2)
        }
        sizeOf100View.needHiddenImage = true
    
        view.addSubview(FFSetHeaderView())
        view.addSubview(sizeOf80View)
        view.addSubview(sizeOf90View)
        view.addSubview(sizeOf100View)
        
        list.append(sizeOf80View)
        list.append(sizeOf90View)
        list.append(sizeOf100View)
        
        let size = FFStoreHelper.share.FFGetSelectFontSize()
        for i in 0 ..< sizeArr.count {
            if size == sizeArr[i] {
                FFSelectChange(i)
                break
            }
        }
    }
    
    fileprivate func FFSelectChange(_ index: Int) {
        if index != selectIndex {
            list[selectIndex].needHiddenImage = true
            list[index].needHiddenImage = false
            selectIndex = index
        }
    }

}

extension FFFontSizeViewController {
    
    /// 设置TableView 的 headerTableView
    private func FFSetHeaderView() -> UIView {
        let vi:UIView = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 44 * ScaleOf375))
        vi.backgroundColor = UIColor.white
        topView = vi
        
        titleLabel = UILabel(frame: vi.bounds)
        titleLabel.text = "字号"
//        titleLabel.font = UIFont.systemFont(ofSize: 20 * ScaleOf375, weight: .medium)
        titleLabel.textAlignment = .center
        vi.addSubview(titleLabel)
        
        completeBtn = UIButton(type: .custom)
//        btn.frame = CGRect(x: ScreenWidth - 60 * ScaleOf375 , y: 7 * ScaleOf375, width: 60 * ScaleOf375, height: 30 * ScaleOf375)
        completeBtn.setTitle("完成", for: .normal)
        completeBtn.titleLabel?.font = UIFont.systemFont(ofSize: 20 * ScaleOf375, weight: .medium)
        completeBtn.setTitleColor(UIColor.black, for: .normal)
        completeBtn.addTarget(self, action: #selector(FFCancelPresent), for: .touchUpInside)
        vi.addSubview(completeBtn)
        
        return vi
    }
    
    /// 取消模态
    @objc private func FFCancelPresent() {
        dismiss(animated: true) {
            FFStoreHelper.share.FFSetUpSelectFontSize(size: self.sizeArr[self.selectIndex])
            NotificationCenter.default.post(Notification(name: Notification.Name.init(UpdateFontSize)))
        };
    }

    
}


/******/

class FFAcquireSimpleView:FFView {
    var size:Int = 0
    var aImageView:UIImageView!
    var block:FFBlock!
    var needHiddenImage:Bool = false {
        willSet {
            self.aImageView.isHidden = newValue
        }
    }
    
    
    override func FFInit() {
    }
    
    convenience init(_ size:Int, _ block: @escaping FFBlock) {
        self.init()
        self.size = size
        self.block = block
        addSubview(FFSimpleView(size))
    }
    
    fileprivate func FFSimpleView(_ size:Int) -> UIView {
        let vi = UIView(frame: CGRect(x: 0, y: 0, width: 180 * ScaleOf375, height: 120 * ScaleOf375))
        vi.backgroundColor = UIColor.white
        vi.layer.cornerRadius = 10 * ScaleOf375
        vi.layer.masksToBounds = true
        
        let leftLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 90 * ScaleOf375, height: 120 * ScaleOf375))
        leftLabel.text = "0"
        leftLabel.font = UIFont.systemFont(ofSize: CGFloat(size) * ScaleOf375, weight: .medium)
        leftLabel.textAlignment = .center
        vi.addSubview(leftLabel)
        
        let rightLabel = UILabel(frame: CGRect(x: 90 * ScaleOf375, y: 0, width: 90 * ScaleOf375, height: 120 * ScaleOf375))
        rightLabel.text = "0"
        rightLabel.font = UIFont.systemFont(ofSize: CGFloat(size) * ScaleOf375, weight: .medium)
        rightLabel.textAlignment = .center
        vi.addSubview(rightLabel)
        
        aImageView = UIImageView(image: UIImage(named: "selected"))
        aImageView.frame = CGRect(x: vi.bounds.size.width - 40, y: 0, width: 40, height: 40)
        vi.addSubview(aImageView)
        
        let btn = UIButton(type: .custom)
        btn.frame = vi.bounds
        btn.addTarget(self, action: #selector(FFUpdateSize(_:)), for: .touchUpInside)
        vi.addSubview(btn)
        
        return vi
    }
    
    @objc fileprivate func FFUpdateSize(_ sender : UIButton) {
        if aImageView.isHidden == true {
            aImageView.isHidden = !aImageView.isHidden
            block()
        }
    }

}
